package fr.eisti.icc.data;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.codehaus.jackson.annotate.JsonIgnore;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author clion
 */
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@Table(name="EMPRUNTEUR")
@XmlRootElement
public abstract class Emprunteur implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name="id")
    private int id;
    @Column(name="nom")
    private String nom;
    @Column(name="prenom")
    private String prenom;
    @Column(name="login")
    private String login;
    @Column(name="numCarte")
    private String numCarte;
    @Column(name="adresse")
    private String adresse;
    @Column(name="tel")
    private String tel;
    @Column(name="dateNaissance")
    private String dateNaissance;
    @Column(name="estBlackListe")
    private boolean estBlackListe;
    @OneToMany(cascade=CascadeType.ALL)
    private List<Emprunt> emprunts;

    public Emprunteur(){

    }

    public Emprunteur(String nom, String prenom, String login, String numCarte, String adresse, String tel, String dateNaissance, boolean estBlackListe, List<Emprunt> emprunts) {
        this.nom = nom;
        this.prenom = prenom;
        this.login = login;
        this.numCarte = numCarte;
        this.adresse = adresse;
        this.tel = tel;
        this.dateNaissance = dateNaissance;
        this.estBlackListe = estBlackListe;
        this.emprunts = emprunts;
    }

    public String getNom() {
        return nom;
    }

    public String getPrenom() {
        return prenom;
    }
    
    public String getLogin() {
        return login;
    }
    

    public String getNumCarte() {
        return numCarte;
    }

    public String getAdresse() {
        return adresse;
    }

    public String getTel() {
        return tel;
    }

    public String getDateNaissance() {
        return dateNaissance;
    }

    public boolean getEstBlackListe() {
        return estBlackListe;
    }

    public void setEstBlackListe(boolean estBlackListe) {
        this.estBlackListe = estBlackListe;
    }

    @XmlTransient
    @JsonIgnore
    public List<Emprunt> getEmprunts() {
        return emprunts;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setNumCarte(String numCarte) {
        this.numCarte = numCarte;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setDateNaissance(String dateNaissance) {
        this.dateNaissance = dateNaissance;
    }

    public void setEmprunts(List<Emprunt> emprunts) {
        this.emprunts = emprunts;
    }

    public boolean addEmprunt(Emprunt emp){

        return  emprunts.add(emp);
    }
    public boolean removeEmprunt(Document doc){
        for (int i = 0; i < emprunts.size(); i++) {
            if(emprunts.get(i).getDocument().equals(doc)){
                return  emprunts.remove(emprunts.get(i));
            }
        }
        return  false;
    }
    public boolean peutEmprunter(){
        return  emprunts.size()<5 && !estBlackListe;
    }
    
}
